﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using System.Diagnostics;
using iTextSharp.text;

namespace PDFMerge
{
    /// <summary>
    /// SimplePDFDoc is a small abstraction of the properties of a PDF document.
    /// This class was used to simplify the application without going too deep into iText library 
    /// (although all data is from iText API calls).
    /// </summary>
    public class SimplePDFDoc
    {
        public int NumPages { get; set; } //Number of pages in the PDF document
        public String FilePath { get; set; } //File path to the PDF document
        public String PageSize { get; set; } //Size of the very first page in the PDF document
        private bool FromScanner { get; set; } // Remove the temp files that were created when disposed
        public String FriendlyName { get; set; } //Friendly name if imported from scanner or image

        //Deconstructor to remove temp scanned PDF files after program exits or PDF is removed from queue
        ~SimplePDFDoc()
        {
            //Make sure we only delete files that were scanned or imported (very important)!
            if (FromScanner)
            {
                try
                {
                    //File Removal can error out if document is open moved or deleted - silently fail
                    System.IO.File.Delete(FilePath);
                }
                catch (Exception) { }
                {         }
                
            }
        }

        #region Constructors
        #endregion
        /// <summary>
        /// Creates a new SimplePDFDoc based off the input file path
        /// Note: pagesize is divided by 72 for height and width
        /// </summary>
        /// <param name="_filePath">The location of the PDF file</param>
        public SimplePDFDoc(string _filePath)
        {
            PdfReader reader = new PdfReader(_filePath);
            Rectangle size = reader.GetPageSizeWithRotation(1);

            PageSize = String.Format("{0}x{1}",
                size.Width / 72,size.Height / 72);
            
            this.FilePath = _filePath;
            this.NumPages = reader.NumberOfPages;

            reader.Close();
            reader = null;
        }

        /// <summary>
        /// Creates a new SimplePDFDoc from a given file path and flags that object was created from scanner/imported
        /// to be removed by the application when no longer needed
        /// </summary>
        /// <param name="_filePath">Path to the PDF document</param>
        /// <param name="Name">The friendly name of the document</param>
        public SimplePDFDoc(string _filePath, string _Name)
        {
            PdfReader reader = new PdfReader(_filePath);
            Rectangle size = reader.GetPageSizeWithRotation(1);

            PageSize = String.Format("{0}x{1}",
                size.Width / 72, size.Height / 72);

            this.FilePath = _filePath;
            this.NumPages = reader.NumberOfPages;

            this.FromScanner = true;
            this.FriendlyName = _Name;

            reader.Close();
            reader = null;
        }

        /// <summary>
        /// Tells the OS to open the PDF document 
        /// this call varies on how your OS is set to handle PDF extensions
        /// </summary>
        public void OpenDocument()
        {
            Process proc = new Process();
            proc.StartInfo.FileName = FilePath;
            proc.Start();
        }
    }
}
